var App = jQuery.fn.extend({
	
    /**
     * Processa envio de formularios
     */
    formSave: function(elmForm, myFunction) {
        var form   = jQuery(elmForm);
        var params = form.serialize();
        var action = form.attr('action');

        jQuery.ajax({
            cache      : false,
            contentType: 'application/x-www-form-urlencoded;charset=utf-8',
            dataType   : 'json',
            type       : 'post',
            url        : action,
            data       : 'action=insert&' +params, 		  
            beforeSend : function(xhr) {		
                //TODO
            },		   
            complete   : function(xhr) {
                //TODO
            },
            error      : function(xhr) {
                alert('Erro ao executar!\n' +xhr.status);
            },
            success    : function(data) {
                if (data.error === true) 
                {
                    alert(data.message);
                }
                else if (data.error === false) 
                {
                    alert(data.message);

                    if (typeof myFunction != 'undefined')
                    {
                        eval(myFunction);
                    }
                }
                else 
                {
                    alert('Erro ao executar!');
                }
            }			
        });			
    },
    
    /**
     * Processa envio de formularios
     */
    formUpdate: function(elmForm, myFunction) {
        var form   = jQuery(elmForm);
        var params = form.serialize();
        var action = form.attr('action');

        jQuery.ajax({
            cache      : false,
            contentType: 'application/x-www-form-urlencoded;charset=ISO-8859-15',
            dataType   : 'json',
            type       : 'post',
            url        : action,
            data       : 'action=update&' +params, 		  
            beforeSend : function(xhr) {		
                //TODO
            },		   
            complete   : function(xhr) {
                //TODO
            },
            error      : function(xhr) {
                alert('Erro ao executar!\n' +xhr.status);
            },
            success    : function(data) {
                if (data.error === true) 
                {
                    alert(data.message);
                }
                else if (data.error === false) 
                {
                    alert(data.message);

                    if (typeof myFunction != 'undefined')
                    {
                        eval(myFunction);
                    }
                }
                else 
                {
                    alert('Erro ao executar!');
                }
            }			
        });			
    },    
    
    /**
     * Processa envio de formularios
     */
    formLogin: function(elmForm) {
        var elmMsg = jQuery('#message');
        var form   = jQuery(elmForm);
        var params = form.serialize();
        var action = form.attr('action');

        jQuery.ajax({
            cache      : false,
            contentType: 'application/x-www-form-urlencoded;charset=ISO-8859-15',
            dataType   : 'json',
            type       : 'post',
            url        : action,
            data       : 'action=login&' +params, 		  
            beforeSend : function(xhr) {		
                elmMsg.css({'color':'orange'}).html('Processando...').slideDown('slow');
            },
            error      : function(xhr) {
                elmMsg.css({'color':'red'}).html('Erro ao executar!').slideDown('slow');
            },
            success    : function(data) {
                if (data.error === true) 
                {
                    elmMsg.css({'color':'red'}).html(data.message).slideDown('slow');
                    $('input:first').focus();
                }
                else if (data.error === false) 
                {
                    elmMsg.css({'color':'green'}).html(data.message).slideDown('slow');
                    App.pageRedirect('admin.jsp');
                }
                else 
                {
                    elmMsg.css({'color':'red'}).html('Erro ao processar retorno!').slideDown('slow');
                }
            }			
        });			
    },        

    /**
     * Refresh actual page
     */
    pageRefresh: function(){
        window.location.href = unescape(window.location.pathname);
    },

    /**
     * Back to last page
     */
    pageBack: function(){
        history.back();
    },

    /**
     * Redirect to set page
     */
    pageRedirect: function(url){
        window.location.href = unescape(url);
    }	
});